package com.stiff.search.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.stiff.common.service.ApiService;
import com.stiff.search.pojo.Item;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ItemService {
    private static final ObjectMapper MAPPER = new ObjectMapper();
    @Autowired
    private ApiService apiService;
    @Value("${TAOTAO_MANAGE_URL}")
    private String TAOTAO_MANAGE_URL;
    @Value("${ITEM_URL}")
    private String ITEM_URL;
    public Item queryItemById(Long itemId){
        String url = TAOTAO_MANAGE_URL + ITEM_URL +"/" + itemId;
        try {
            String jsonDate = this.apiService.doGet(url);
            if(StringUtils.isEmpty(jsonDate)){
                return null;
            }
            return MAPPER.readValue(jsonDate,Item.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
